
// 战斗事件限制执行次数。
// 注释：<Times Limited: x, y>
// x：触发事件的次数
// y：计数条件（0：胜利，1：逃跑，2：失败，3：任何战斗结束情况），其它情况：战斗结束不计数
// 手动计数方法：$gameSystem.updateBattleEventTimes($gameTroop._troopId);
// $gameSystem.curTroopEventTimesLimit() // 当前敌群时间次数上限
// $gameSystem.curTroopEventTimes()  // 当前敌群时间次数



// 限次战斗事件的条件
Game_Troop.prototype.meetsTimesLimitedEventCompleted = function(){
    if ($gameSystem.isBattleEventTimesUnderLimit(this._troopId))
        return false;
    else
        return true;
};

let SWDA_TimesLimitedBattleEvent_setup = Game_Troop.prototype.setup;
Game_Troop.prototype.setup = function(troopId){
	SWDA_TimesLimitedBattleEvent_setup.call(this, troopId);
	this.setupTimesLimitedEvent();
};

//注释：<Times Limited: x, y >，x：限定x次数，y：计数条件（0：胜利，1：逃跑，2：失败，3：任何战斗结束情况，其它情况：战斗结束不计数）
Game_Troop.prototype.setupTimesLimitedEvent = function(){
	const page = this.troop().pages[0];
	const reg = /<Times Limited: (\d+), (\d*)>/;
    this._isEventTimesLimited = false;

	page.list.forEach(function(ev){
		if([108, 408].contains(ev.code)){
			if(reg.exec(ev.parameters[0]) ){
                $gameSystem.setBattleEventTimesLimit(this._troopId ,Number(RegExp.$1), Number(RegExp.$2) );
                this._isEventTimesLimited = true;
            }
		}
	}, this);
};

Game_System.prototype.setBattleEventTimesLimit = function(troopId, n, condition) {
	if (!this._troopEventTimesLimit) {
        this._troopEventTimesLimit = [];
    }
    this._troopEventTimesLimit[troopId] = n;

    if (!this._troopEventTimesLimitCondition) {
        this._troopEventTimesLimitCondition = [];
    }
    this._troopEventTimesLimitCondition[troopId] = condition;

    
    if (!this._troopEventTimes) {
        this._troopEventTimes = [];
    }

    if (!this._troopEventTimes[troopId]) {
		this._troopEventTimes[troopId] = 0;
	}

};

// 当前敌群时间次数上限
Game_System.prototype.curTroopEventTimesLimit = function(){
    return $gameSystem._troopEventTimesLimit[$gameTroop._troopId];
};

// 当前敌群时间次数
Game_System.prototype.curTroopEventTimes = function(){
    return $gameSystem._troopEventTimes[$gameTroop._troopId];
};

Game_System.prototype.updateBattleEventTimes = function(troopId) {
    if (this.isBattleEventTimesUnderLimit(troopId))
        this._troopEventTimes[troopId]++;
};

Game_System.prototype.isBattleEventTimesUnderLimit = function(troopId) {
    return this._troopEventTimes[troopId] < this._troopEventTimesLimit[troopId];
};

let SWDA_BattleManager_endBattle = BattleManager.endBattle;
BattleManager.endBattle = function(result) {
    const troopId = $gameTroop._troopId;
    if ($gameTroop._isEventTimesLimited && $gameSystem.isBattleEventTimesUnderLimit(troopId)){
        const condition= $gameSystem._troopEventTimesLimitCondition[troopId];
        if (condition === 3){                               // 任何战斗结束情况
            $gameSystem.updateBattleEventTimes(troopId);
        } else if (result === 0 && condition === 0) {
            $gameSystem.updateBattleEventTimes(troopId);    // 胜利
          } else if (result === 1 && condition === 1) {
            $gameSystem.updateBattleEventTimes(troopId);    // 逃跑
          } else if (result === 2 && condition === 2) {
            $gameSystem.updateBattleEventTimes(troopId);    // 失败
          }
    }

    SWDA_BattleManager_endBattle.call(this, result);
};

// 插件指令
let SWDA_TimesLimitedBattleEvent_PLUGIN_COMMAND = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function(command, args) {
    SWDA_TimesLimitedBattleEvent_PLUGIN_COMMAND.call(this, command, args);
    if(command == 'addBattleEventTimes'){
        $gameSystem.updateBattleEventTimes($gameTroop._troopId);
    }
};